
package com.rx.mgt.service;  

import java.util.List;

import com.rx.mgt.dao.*;
import com.rx.mgt.domain.Group;
import com.rx.mgt.domain.SysOpreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.rx.mgt.domain.Domain;
import com.rx.mgt.domain.DomainInfoExt;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.util.DateUtil;

/** 
 * ClassName:DomainInfoExtService 
 * Function :
 * Reason   : 
 * Date     :Feb 1, 2017 10:30:32 AM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Service
public class DomainInfoExtService {
    @Autowired
    DomainInfoExtDao domainInfoExtDao;
    @Autowired
    DomainDao domainDao;
    @Autowired
    SysOpreateDao sysOpreateDao;
    @Autowired
    private SpamDao spamDao;
    @Autowired
    GroupDao groupDao;
    @Autowired
    DomainSignDao domainSignDao;

    public List<DomainInfoExt> getDomainInfoExt(UsersBo usersBo){
        return domainInfoExtDao.getDomainInfoExt(usersBo);
    }
    public Long getDomainInfoExtCount(UsersBo usersBo){
        return domainInfoExtDao.getDomainInfoExtCount(usersBo);
    }
    public DomainInfoExt get(Long id){
        return domainInfoExtDao.get(id);
    }
    @Transactional
    public List<DomainInfoExt> list(){
        return domainInfoExtDao.list();
    }
    @Transactional
    public void add(DomainInfoExt domainInfoExt){
        domainInfoExt.setCreate_time(DateUtil.nowTimeString());
        domainInfoExt.setStatus("N");
        domainInfoExtDao.save(domainInfoExt);
        Domain domain= domainDao.get(domainInfoExt.getDomain());
        if(domain==null){
            domain=new Domain();
            domain.setDomain_name(domainInfoExt.getDomain());
            domainDao.save(domain);
            SysOpreate s = new SysOpreate();
            s.setDomain(domainInfoExt.getDomain());
            s.setDomainsign_status("1");
            s.setReplay_status("1");
            s.setSendcount_status("1");
            s.setVinus_status("1");
            s.setSpam_status("1");
            s.setWhiteblack_status("1");
            s.setDomainsign_status("1");
            sysOpreateDao.save(s);
            Group group = new Group();
            group.setDomain(domainInfoExt.getDomain());
            group.setpId(0l);
            group.setIs_root(1);
            group.setName(domainInfoExt.getCompany());
            groupDao.save(group);
        }
    }
    @Transactional
    public void delete(Long id){
        DomainInfoExt domainInfoExt = domainInfoExtDao.get(id);
        Domain domain = domainDao.selectDomainByDomain(domainInfoExt.getDomain());
        if(domain!=null) {
            domainDao.delete(domain);
        }
        domainInfoExtDao.delete(domainInfoExt);
        //综合功能开关
        sysOpreateDao.deleteByDomain(domainInfoExt.getDomain());
        //删除反垃圾
        spamDao.deleteByDomain(domainInfoExt.getDomain());
        //域签名



    }
    @Transactional
    public void update(DomainInfoExt domainInfoExt){
        domainInfoExtDao.update(domainInfoExt);
        if("N".equals(domainInfoExt.getStatus())){
            Domain domain =domainDao.get(domainInfoExt.getDomain());
            if(domain!=null){
                domainDao.delete(domain);
            }
        }else{
            Domain domain = domainDao.get(domainInfoExt.getDomain());
            if(domain==null){
                domain=new Domain();
                domain.setDomain_name(domainInfoExt.getDomain());
                domainDao.save(domain);
            }
        }
    }

    @Transactional
    public DomainInfoExt getDomainInfoExtByDomain(String domain) {

        return domainInfoExtDao.getDomainInfoExtByDomain(domain);
    }

    @Transactional
    public DomainInfoExt getDomainInfoExtByid(String id) {

        return domainInfoExtDao.getDomainInfoExtById(id);
    }

    @Transactional(readOnly =  false,propagation = Propagation.REQUIRED)
    public String updateDomainInfoExtTimeAndCount(DomainInfoExt domainInfoExt) {
        DomainInfoExt e = domainInfoExtDao.get(domainInfoExt.getId());
        e.setTime(domainInfoExt.getTime());
        e.setMax_send_count(domainInfoExt.getMax_send_count());
        domainInfoExtDao.update(e);
        return e.getDomain();
    }
}
  